﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using API;
using Foundation;
using mb;
using mb.iOS;
using MyExtensions;
using UIKit;

namespace libmp
{
    public class VCMovie : UIViewController
    {
        ILog logger = mb.ServiceLocator.Instance.Get<ILog>();
        UITableView TabMovies;

        public VCMovie() : base("MovieList", GlobalImpl.ResBundle)
        {
            TabBarItem = new UITabBarItem("电影", null, 0);
        }

        public override void ViewDidLayoutSubviews() { }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            View.BackgroundColor = UIColor.Orange;

            TabMovies = View.ViewWithTag(101) as UITableView;

            var viewSize = View.Frame;

            logger.Log("VCMovie ViewDidLoad", viewSize);

            var data = new List<string>() { "ios", "android", "uwp", "linux", "macos" };
            for (int i = 0; i < 10; i++)
                data.Insert(0, i.ToString());
            TabMovies.Source = new TableSource(data.ToArray());

            var btn = View.GetSubViewByAID("btnReload") as UIButton;
            btn.TouchUpInside += (s, e) =>
            {
                logger.Log("click reload");
                data.RemoveAt(0);

                TabMovies.Source = new TableSource(data.ToArray());
                TabMovies.ReloadData();
            };

            // TabMovies.Source.RowSelected(TabMovies, NSIndexPath.FromRowSection(0, 0));
        }

    }
    class TableSource : UITableViewSource
    {
        string[] list;
        public TableSource() { }
        public TableSource(string[] list)
        {
            this.list = list;
        }
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = new UITableViewCell(UITableViewCellStyle.Default, "");
            string item = list[indexPath.Row];
            cell.TextLabel.Text = item;
            return cell;
        }
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return list.Length;
        }
        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            string item = list[indexPath.Row];
            var appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
            appDelegate.NavController.PushViewController(new VCMoviePlayer(), false);
        }
    }
}